﻿using parkMonitor.entity;
using parkMonitor.model;
using parkMonitor.server.uiLogServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using parkMonitor.DataBase;

namespace parkMonitor
{
    public class ManualParkingSimul
    {
        public static ManualParkingSimul ins;
        private const string userId = "18202736439";
        private const string garageId = "1";
        private const string header = "鄂A";
        private const int licInterval = 60000;//90秒
        private Random rnd;
        private Dictionary<int, CarStatusStru> numStatusMap = new Dictionary<int, CarStatusStru>();
        Command storeCmd = new Command();
        IEquipments queue;
        DBOperation oper = new DBOperation();

        public ManualParkingSimul()
        {
            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    numStatusMap.Add(i, new CarStatusStru(header + (i * 11111).ToString("D5"), 0));
                }
                while (queue == null)
                {
                    queue = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.Queue);
                }
            }).Wait();
            Task.Factory.StartNew(() =>
            {
                Run();
            });
        }

        public void Store(string lic)
        {
            string connectionStr = "SqlConnectionLocation";
            storeCmd.manual = true;
            storeCmd.commandType = 's';
            storeCmd.userID = oper.GetUserID(connectionStr,userId).ToString();
            storeCmd.LicenseNum = lic;
            try
            {
                storeCmd.garageID = Convert.ToInt32(garageId);
            }
            catch { UILogServer.ins.error("非法车库号，请重新输入"); return; }
            int garageFreeSpace = 0;
            bool isTelRegister = oper.IsTelRegister(connectionStr,userId);
            if (isTelRegister == false)
            {
                UILogServer.ins.error("该号码未被注册，请先注册");
            }
            else if (storeCmd.garageID != 0)
            {
                garageFreeSpace = oper.getGarageFreeSpace(connectionStr,storeCmd.garageID);
                if (garageFreeSpace > 0)
                {
                    if (storeCmd.userID != "" && storeCmd.LicenseNum != "")
                    {
                        queue.SetMessage((Command)storeCmd.Clone());
                        UILogServer.ins.info("发送成功");
                    }
                }
                else
                {
                    UILogServer.ins.error("该车库没有剩余车位");
                }
            }
        }

        public void Fetch(string lic)
        {
            string connectionStr = "SqlConnectionLocation";
            storeCmd.manual = true;
            storeCmd.commandType = 'f';
            storeCmd.userID = oper.GetUserID(connectionStr,userId).ToString();
            storeCmd.LicenseNum = lic;
            storeCmd.TimeRecord = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            try
            {
                storeCmd.garageID = Convert.ToInt32(garageId);
            }
            catch { UILogServer.ins.error("非法车库号，请重新输入"); return; }
            bool isTelRegister = oper.IsTelRegister(connectionStr,userId);
            bool isNumberPlate = oper.IsNumberPlate(connectionStr,storeCmd.LicenseNum, storeCmd.garageID);
            if (isTelRegister == false)
            {
                UILogServer.ins.error("该号码未被注册，请先注册");
            }
            else if (isNumberPlate == false)
            {
                UILogServer.ins.error("车库中查无此车");
            }
            else
            {
                storeCmd.parkingRecordsID = oper.GetParkingRecordsID(connectionStr,storeCmd.LicenseNum);
                if (storeCmd.userID != "" && storeCmd.LicenseNum != "" && storeCmd.parkingRecordsID != 0)
                {
                    queue.SetMessage((Command)storeCmd.Clone());
                    UILogServer.ins.info("发送成功");
                }
                else
                {
                    UILogServer.ins.error("电话号码或号牌错误");
                }
            }

        }

        public void Run()
        {
            while (true)
            {
                rnd = new Random(DateTime.Now.Millisecond);
                if (numStatusMap.Count > 0)
                {
                    int index = rnd.Next(0, numStatusMap.Count);
                    int status = numStatusMap[index].status;
                    if(status == 0)
                    {
                        Store(numStatusMap[index].licNum);
                        numStatusMap[index].status = 2;
                    }
                    else if(status == 1)
                    {
                        Fetch(numStatusMap[index].licNum);
                        numStatusMap[index].status = 3;
                    }
                }
                Thread.Sleep(licInterval);
            }
        }

        public void Update(int index)
        {
            int status = numStatusMap[index].status;
            if (status == 2)
            {
                numStatusMap[index].status = 1;
            }
            else if(status == 3)
            {
                numStatusMap[index].status = 0;
            }
        }
    }
    class CarStatusStru
    {
        public string licNum { get; set; }
        public int status { get; set; }
        public CarStatusStru()
        {
            licNum = "";
            status = 0;
        }
        public CarStatusStru(string str, int stat)
        {
            licNum = str;
            status = stat;
        }
    }

}
